/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SalasDisponiveis.java
 *
 * Created on Jul 8, 2011, 4:15:38 AM
 */

package janelas;

import classes.Equipamento;
import classes.Reserva;
import classes.Sala;
import db.EquipDAO;
import db.ReservaDAO;
import db.SalaDAO;
import java.awt.Color;
import java.awt.Component;
import java.lang.String;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import projetofinaltecnicas.Main;

/**
 * 
 * @author Matheus Freire
 */

public class SalasDisponiveis extends javax.swing.JPanel {

    String[] dias  = new String[7];
    String[] tabela = new String[4];
    String[] equip = new String[2];
    String[] hora = {"8","10","12","13","15","17","19","21"};
    String[] data = new String[7];
    String[] status = new String[3];
    int diasnum[] = {1,2,3,4,5,6,7};
    String idioma;
    String usuario;

    List<Reserva> listaR;

    int idSala;
    int diaatual;
    /** Creates new form SalasDisponiveis */
    public SalasDisponiveis(String idioma, String usuario) {
        initComponents();
        
        ResourceBundle palavras = ResourceBundle.getBundle("idiomas.Lingua_"+idioma);
        dias[0] = palavras.getString("semanaHeader.domingo");
        dias[1] = palavras.getString("semanaHeader.segunda");
        dias[2] = palavras.getString("semanaHeader.terca");
        dias[3] = palavras.getString("semanaHeader.quarta");
        dias[4] = palavras.getString("semanaHeader.quinta");
        dias[5] = palavras.getString("semanaHeader.sexta");
        dias[6] = palavras.getString("semanaHeader.sabado");

        tabela[0] = palavras.getString("salasHeader.id");
        tabela[1] = palavras.getString("salasHeader.lugares");
        tabela[2] = palavras.getString("salasHeader.local");
        tabela[3] = palavras.getString("salasHeader.tipo");

        status[0] = palavras.getString("salasDispo.statusPen");
        status[1] = palavras.getString("salasDispo.statusDispo");
        status[2] = palavras.getString("salasDispo.statusRes");

        titulo.setText(palavras.getString("salasDispo.titulo"));
        juser.setText(palavras.getString("salasSolicitar.user"));
        jiduser.setText(usuario);
        disponibilidade.setText(palavras.getString("salasDispo.verificar"));
        jVerificar.setText(palavras.getString("salasDispo.verificar"));

        equip[0] = palavras.getString("sequipHeader.id");
        equip[1] = palavras.getString("sequipHeader.nome");

        organizarHeader();
        organizarHeaderSalas();
        organizarHeaderEquip();

        this.idioma = idioma;
        this.usuario = usuario;

        datasemana(diaatual);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popsala = new javax.swing.JPopupMenu();
        disponibilidade = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tabelasala = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        semana = new javax.swing.JTable();
        jIdentifica = new javax.swing.JLabel();
        jAnterior = new javax.swing.JButton();
        jProximo = new javax.swing.JButton();
        titulo = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        juser = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelaequip = new javax.swing.JTable();
        jiduser = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jVerificar = new javax.swing.JButton();

        disponibilidade.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/find.png"))); // NOI18N
        disponibilidade.setText("Verificar Disponibilidade");
        disponibilidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                disponibilidadeActionPerformed(evt);
            }
        });
        popsala.add(disponibilidade);

        setPreferredSize(new java.awt.Dimension(776, 552));
        setSize(new java.awt.Dimension(776, 552));
        addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                formComponentAdded(evt);
            }
        });

        jPanel1.setPreferredSize(new java.awt.Dimension(776, 552));
        jPanel1.setSize(new java.awt.Dimension(776, 552));
        jPanel1.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                jPanel1formComponentAdded(evt);
            }
        });

        tabelasala.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "ID", "N Lugares", "Local", "TIpo"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelasala.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabelasala.setShowGrid(true);
        tabelasala.setShowHorizontalLines(false);
        tabelasala.getTableHeader().setReorderingAllowed(false);
        tabelasala.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelasalaMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tabelasala);
        tabelasala.getColumnModel().getColumn(0).setResizable(false);
        tabelasala.getColumnModel().getColumn(1).setResizable(false);
        tabelasala.getColumnModel().getColumn(2).setResizable(false);
        tabelasala.getColumnModel().getColumn(3).setResizable(false);

        semana.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {"08:00-09:50", null, null, null, null, null, null, null},
                {"10:00-11:50", null, null, null, null, null, null, null},
                {"11:50-13:30", null, null, null, null, null, null, null},
                {"13:30-15:20", null, null, null, null, null, null, null},
                {"15:30-17:20", null, null, null, null, null, null, null},
                {"17:30-19:20", null, null, null, null, null, null, null},
                {"19:30-21:20", null, null, null, null, null, null, null}
            },
            new String [] {
                "", "Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        semana.setColumnSelectionAllowed(true);
        semana.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        semana.setShowGrid(true);
        semana.getTableHeader().setReorderingAllowed(false);
        semana.setVerifyInputWhenFocusTarget(false);
        jScrollPane1.setViewportView(semana);
        semana.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        semana.getColumnModel().getColumn(0).setResizable(false);
        semana.getColumnModel().getColumn(1).setResizable(false);
        semana.getColumnModel().getColumn(2).setResizable(false);
        semana.getColumnModel().getColumn(3).setResizable(false);
        semana.getColumnModel().getColumn(4).setResizable(false);
        semana.getColumnModel().getColumn(5).setResizable(false);
        semana.getColumnModel().getColumn(6).setResizable(false);
        semana.getColumnModel().getColumn(7).setResizable(false);

        jIdentifica.setText("Identificador");

        jAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/magifier_zoom_out.png"))); // NOI18N
        jAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAnteriorActionPerformed(evt);
            }
        });

        jProximo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/magnifier_zoom_in.png"))); // NOI18N
        jProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jProximoActionPerformed(evt);
            }
        });

        titulo.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        titulo.setText("Solicitar Sala");

        juser.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        juser.setText("Usuário");

        tabelaequip.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID Equipamento", "Nome"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaequip.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabelaequip.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tabelaequip);
        tabelaequip.getColumnModel().getColumn(0).setResizable(false);
        tabelaequip.getColumnModel().getColumn(1).setResizable(false);

        jiduser.setText("ID User");

        jVerificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/find.png"))); // NOI18N
        jVerificar.setText("Verificar Disponibilidade");
        jVerificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jVerificarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 736, Short.MAX_VALUE)
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(jVerificar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 185, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 516, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 512, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)))
                        .addContainerGap())
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(titulo)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 378, Short.MAX_VALUE)
                        .add(juser)
                        .add(18, 18, 18)
                        .add(jiduser)
                        .add(178, 178, 178))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                                .add(jIdentifica)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 588, Short.MAX_VALUE)
                                .add(jAnterior, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jProximo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 736, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(titulo)
                    .add(juser, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jiduser))
                .add(8, 8, 8)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jScrollPane3, 0, 0, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jVerificar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(7, 7, 7)
                        .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 246, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jProximo)
                            .add(jAnterior))
                        .add(8, 8, 8))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jIdentifica)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)))
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 148, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(53, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 776, Short.MAX_VALUE)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(0, 0, Short.MAX_VALUE)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 552, Short.MAX_VALUE)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(0, 0, Short.MAX_VALUE)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(0, 0, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void disponibilidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disponibilidadeActionPerformed
        // TODO add your handling code here:
        jAnterior.setEnabled(true);
        jProximo.setEnabled(true);

        int lin = tabelasala.getSelectedRow();

        jIdentifica.setVisible(true);
        
        if(idioma.equals("en_US")){
            jIdentifica.setText("Classroom: "+tabelasala.getValueAt(lin, 0)+" / Locale: "+tabelasala.getValueAt(lin, 2)
                    +" / Seats: "+tabelasala.getValueAt(lin, 1)+" / Type: "+tabelasala.getValueAt(lin, 3));
        }else{
            jIdentifica.setText("Sala: "+tabelasala.getValueAt(lin, 0)+" / Local: "+tabelasala.getValueAt(lin, 2)
                    +" / Lugares: "+tabelasala.getValueAt(lin, 1)+" / Tipo: "+tabelasala.getValueAt(lin, 3));
        }

        verificarDisponibilidade();
    }//GEN-LAST:event_disponibilidadeActionPerformed

    private void formComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_formComponentAdded
        // TODO add your handling code here:
        semana.setEnabled(false);
        jAnterior.setEnabled(false);
        jProximo.setEnabled(false);

        jIdentifica.setVisible(false);

        Calendar calendar1 = Calendar.getInstance();
        diaatual = calendar1.get(Calendar.DAY_OF_WEEK);

        semana.getColumnModel().getColumn(0).setMinWidth(82);

        List<Sala> lista;

        try{
            SalaDAO saladao = new SalaDAO();

            lista = saladao.salaListar();

            preencherTabela(lista);
        }
        catch(SQLException ex){
            ex.printStackTrace();
        }
    }//GEN-LAST:event_formComponentAdded

    private void tabelasalaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelasalaMouseClicked
        // TODO add your handling code here:
        int linha=0;
        int coluna=0;

        linha = tabelasala.getSelectedRow();

        if(evt.getButton() == java.awt.event.MouseEvent.BUTTON1 && evt.getClickCount() == 2){
            popsala.show(tabelasala, evt.getX() , evt.getY());
            idSala = Integer.parseInt(tabelasala.getModel().getValueAt(linha, coluna).toString());
        }else{
            idSala = Integer.parseInt(tabelasala.getModel().getValueAt(linha, coluna).toString());
        }
}//GEN-LAST:event_tabelasalaMouseClicked

    private void jAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAnteriorActionPerformed
        // TODO add your handling code here:
        String dia1;
        String dia2;
        String diaP;

        diaatual++;

        diaP = dias[6];

        for(int i=6;i>=0;i--){
            if(i>0){
                dia1 = dias[i-1];
                dias[i] = dia1;
            }else{
                dias[i] = diaP;
            }
        }

        datasemana(diaatual);
        organizarHeader();
        verificarDisponibilidade();
}//GEN-LAST:event_jAnteriorActionPerformed

    private void jProximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jProximoActionPerformed
        // TODO add your handling code here:
        String dia1;
        String diaP;

        diaatual--;

        diaP = dias[0];

        for(int i=0;i<7;i++){
            if(i<6){
                dia1 = dias[i+1];
                dias[i] = dia1;
            }else{
                dias[i] = diaP;
            }
        }

        datasemana(diaatual);
        organizarHeader();
        verificarDisponibilidade();
}//GEN-LAST:event_jProximoActionPerformed

    private void jPanel1formComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_jPanel1formComponentAdded
        // TODO add your handling code here:

        semana.setEnabled(false);
        jAnterior.setEnabled(false);
        jProximo.setEnabled(false);

        jIdentifica.setVisible(false);

        Calendar calendar1 = Calendar.getInstance();
        diaatual = calendar1.get(Calendar.DAY_OF_WEEK);

        semana.getColumnModel().getColumn(0).setMinWidth(82);

        List<Sala> lista;

        try{
            SalaDAO saladao = new SalaDAO();

            lista = saladao.salaListar();

            preencherTabela(lista);
        } catch(SQLException ex){
            ex.printStackTrace();
        }
}//GEN-LAST:event_jPanel1formComponentAdded

    private void jVerificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jVerificarActionPerformed
        // TODO add your handling code here:
        int lin = tabelasala.getSelectedRow();

        if(lin==-1){
            if(idioma.equals("en_US")){
                JOptionPane.showMessageDialog(null, "To see a classroom schedule availability, you have to select one!", "ERROR!", JOptionPane.ERROR_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(null, "Para ver o calendário de disponibilidade de um sala, você deve selecionar uma!", "ERRO!", JOptionPane.ERROR_MESSAGE);
            }
        }else{
            jAnterior.setEnabled(true);
            jProximo.setEnabled(true);

            jIdentifica.setVisible(true);

            if(idioma.equals("en_US")){
                jIdentifica.setText("Classroom: "+tabelasala.getValueAt(lin, 0)+" / Locale: "+tabelasala.getValueAt(lin, 2)
                        +" / Seats: "+tabelasala.getValueAt(lin, 1)+" / Type: "+tabelasala.getValueAt(lin, 3));
            }else{
                jIdentifica.setText("Sala: "+tabelasala.getValueAt(lin, 0)+" / Local: "+tabelasala.getValueAt(lin, 2)
                        +" / Lugares: "+tabelasala.getValueAt(lin, 1)+" / Tipo: "+tabelasala.getValueAt(lin, 3));
            }

            Main.log.info("A Sala: "+tabelasala.getValueAt(lin, 0)+" teve sua disponibilidade verificada pelo usuário: "+usuario);

            verificarDisponibilidade();
        }
    }//GEN-LAST:event_jVerificarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem disponibilidade;
    private javax.swing.JButton jAnterior;
    private javax.swing.JLabel jIdentifica;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jProximo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JButton jVerificar;
    private javax.swing.JLabel jiduser;
    private javax.swing.JLabel juser;
    private javax.swing.JPopupMenu popsala;
    private javax.swing.JTable semana;
    private javax.swing.JTable tabelaequip;
    private javax.swing.JTable tabelasala;
    private javax.swing.JLabel titulo;
    // End of variables declaration//GEN-END:variables

    public void preencherTabela(List<Sala> listasala){

        DefaultTableModel tabela = (DefaultTableModel) tabelasala.getModel();

        while (tabela.getRowCount() > 0) { //vai pegar todas as linha selecionadas maiores que zero, é vai remove-las!
            tabela.removeRow(0);
        }

       String[] linha = new String[]{null, null, null, null}; //colocando null em todas as colunas
        for (int i = 0; i < listasala.size(); i++) {
            tabela.addRow(linha);
            tabela.setValueAt(listasala.get(i).getIdsala(), i, 0);
            tabela.setValueAt(listasala.get(i).getQtdlugares(), i, 1);
            tabela.setValueAt(listasala.get(i).getLocalsala(), i, 2);
            tabela.setValueAt(listasala.get(i).getTiposala(), i, 3);
        }
       
    }

    public void preencherSemana(List<Reserva> lista){

        DefaultTableModel tabela = (DefaultTableModel) semana.getModel();

        String compara;

        for(int j=1;j<8;j++){
            for(int x=1;x<8;x++){
                if(!lista.isEmpty()){
                    for(int i=0;i<lista.size();i++){
                        Date datax = lista.get(i).getData();
                        SimpleDateFormat formato;
                        String dataFormat;

                        if(idioma.equals("en_US")){
                            formato = new SimpleDateFormat("MM-dd-yyyy");
                            dataFormat = formato.format(datax);
                        }else{
                            formato = new SimpleDateFormat("dd-MM-yyyy");
                            dataFormat = formato.format(datax);
                        }

                        if(dataFormat.equals(tabela.getValueAt(0, j))
                           &&(lista.get(i).getHora() == x)){
                           
                            compara = lista.get(i).getStatus();

                            if(compara.equals("PENDENTE")){
                                tabela.setValueAt(status[0], x, j);
                                lista.remove(lista.get(i));
                            }else{
                                tabela.setValueAt(status[2], x, j);
                                lista.remove(lista.get(i));
                            }
                        }
                    }
                }else{
                    tabela.setValueAt(status[1], x, j);
                }
            }
        }
    }
    
    public void preencherSemanaVazia(){

        DefaultTableModel tabela = (DefaultTableModel) semana.getModel();

        for(int x=1;x<8;x++){
            for(int j=1;j<8;j++){
                tabela.setValueAt(status[1], x, j);
            }
        }
    }

    public void mostrar(){
        int linha=0;
        int coluna=0;

        linha = tabelasala.getSelectedRow() + 1;
        coluna = tabelasala.getSelectedColumn() + 1;

        JOptionPane.showMessageDialog(null,"Linha = "+linha+"\nColuna = "+coluna);
    }

    public void organizarHeader(){

        DefaultTableModel model = (DefaultTableModel) semana.getModel();
        JTable table = new JTable(model);

        for(int i=1;i<8;i++){
            semana.getTableHeader().setFont(new java.awt.Font(""+dias[i-1],java.awt.Font.PLAIN, 11));
            semana.getColumnModel().getColumn(i).setHeaderValue(""+dias[i-1]);
        }
    }
    
    public void organizarHeaderSalas(){

        DefaultTableModel model = (DefaultTableModel) tabelasala.getModel();
        JTable table = new JTable(model);

        for(int i=0;i<4;i++){
            tabelasala.getTableHeader().setFont(new java.awt.Font(""+tabela[i],java.awt.Font.PLAIN, 11));
            tabelasala.getColumnModel().getColumn(i).setHeaderValue(""+tabela[i]);
        }
    }

    public void diasSemana(String[] datas){
        DefaultTableModel tabela = (DefaultTableModel) semana.getModel();

        for (int i = 0; i < 7; i++) {
            tabela.setValueAt(datas[i], 0, i+1);
        }

    }

    public void datasemana(int dia){
        SimpleDateFormat formato;
        
        if(idioma.equals("en_US")){
            formato = new SimpleDateFormat("MM-dd-yyyy");
        }else{
            formato = new SimpleDateFormat("dd-MM-yyyy");
        }

        int num=0;

        for(int j=1;j<8;j++){
            if(j<dia){
                num = dia - j;
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DATE, -num);
                data[j-1] = ""+formato.format(calendar.getTime());
            }else{
                if(dia==j){
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.DATE, 0);
                    data[j-1] = ""+formato.format(calendar.getTime());
                }else{
                    num = j - dia;
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.DATE, num);
                    data[j-1] = ""+formato.format(calendar.getTime());
                }
            }
        }

        diasSemana(data);
    }

    public void verificarDisponibilidade(){
        try{
            ReservaDAO resdao = new ReservaDAO();
            Reserva res = new Reserva();
            EquipDAO equipd = new EquipDAO();
            List<Equipamento> lista = new ArrayList<Equipamento>();
            int ids[];

            listaR = resdao.reservaLista(idSala);
            
            ids = equipd.idsEquip(idSala);

            for(int i=0;i<ids.length;i++){
                lista.add(equipd.equipSala(ids[i]));
            }

            if(listaR.isEmpty()){
                preencherSemanaVazia();
                preencherEquip(lista);
                colorirTabela();
            }else{
                preencherSemanaVazia();
                preencherEquip(lista);
                preencherSemana(listaR);
                colorirTabela();
            }
        } catch(SQLException ex){
            ex.printStackTrace();
        }
    }

    public void colorirTabela(){
        for(int row=0;row<8;row++){
            for(int column=0;column<8;column++){
                semana.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
                    public Component getTableCellRendererComponent(JTable table, Object value,
                            boolean isSelected, boolean hasFocus, int row, int column) {
                        super.getTableCellRendererComponent(table, value, isSelected,
                                hasFocus, row, column);

                        if(row==0 && column==0){
                            setBackground(null);
                            setForeground(Color.BLACK);
                        }else{
                            if(semana.getValueAt(row, column).toString().equals(status[1])){
                                setBackground(new java.awt.Color(38, 223, 38));
                                setForeground(Color.BLACK);
                            }else{
                                if(semana.getValueAt(row, column).toString().equals(status[0])){
                                    setBackground(new java.awt.Color(243, 245, 41));
                                    setForeground(Color.BLACK);
                                }else{
                                    if(semana.getValueAt(row, column).toString().equals(status[2])){
                                        setBackground(new java.awt.Color(235, 31, 31));
                                        setForeground(Color.WHITE);
                                    }else{
                                        setBackground(null);
                                        setForeground(Color.BLACK);
                                    }
                                }
                            }
                        }

                        return this;
                    }
                });
            }
        }
    }

    public void preencherEquip(List<Equipamento> lista){
        DefaultTableModel tabela = (DefaultTableModel) tabelaequip.getModel();

        while (tabela.getRowCount() > 0) { //vai pegar todas as linha selecionadas maiores que zero, é vai remove-las!
            tabela.removeRow(0);
        }

        String[] linha = new String[]{null, null}; //colocando null em todas as colunas
        for (int i = 0; i < lista.size(); i++) {
            tabela.addRow(linha);
            tabela.setValueAt(lista.get(i).getIdequip(), i, 0);
            tabela.setValueAt(lista.get(i).getNome(), i, 1);
        }

        descricao(lista);
    }
    
    public void descricao(final List<Equipamento> lista){
        tabelaequip.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected,
                        hasFocus, row, column);

                setToolTipText(lista.get(row).getDescricao());

                return this;
            }
        });
    }

    public void organizarHeaderEquip(){

        DefaultTableModel model = (DefaultTableModel) tabelaequip.getModel();
        JTable table = new JTable(model);

        for(int i=0;i<2;i++){
            tabelaequip.getTableHeader().setFont(new java.awt.Font(""+equip[i],java.awt.Font.PLAIN, 11));
            tabelaequip.getColumnModel().getColumn(i).setHeaderValue(""+equip[i]);
        }
    }
}